package com.ruoyi.web.controller.monitor;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.vo.PreventionSecurityRiskVO;
import com.ruoyi.system.entity.SysMonitor;
import com.ruoyi.system.mapper.SysMonitorMapper;
import com.ruoyi.system.service.SysMonitorService;
import org.apache.commons.collections.map.HashedMap;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 报警信息表(SysMonitor)表控制层
 * @since 2022-11-17 09:40:56
 */
@RestController
@RequestMapping("/sysMonitor")
public class SysMonitorController extends BaseController {
    @Resource
    private SysMonitorService sysMonitorService;
    @Resource
    private SysMonitorMapper sysMonitorMapper;

    /**
     * 分页查询
     *
     * @param sysMonitor 筛选条件
     * @return 查询结果
     */
    @GetMapping("/list")
    public TableDataInfo queryByPage(SysMonitor sysMonitor) {
        startPage();
        List<SysMonitor> list = sysMonitorService.queryByPage(sysMonitor);
        return getDataTable(list);
    }

    /**
     * 通过主键查询单条数据
     *
     * @return 单条数据
     */
    @GetMapping("{id}")
    public AjaxResult queryById(@PathVariable("id") Integer id) {
        return AjaxResult.success(this.sysMonitorService.queryById(id));
    }

    /**
     * 新增数据
     *
     * @return 新增结果
     */
    @PostMapping("/create")
    public AjaxResult add(@RequestBody SysMonitor sysMonitor) {
        return AjaxResult.success(this.sysMonitorService.insert(sysMonitor));
    }

    /**
     * 编辑数据
     *
     * @return 编辑结果
     */
    @PostMapping("/update")
    public AjaxResult edit(@RequestBody SysMonitor sysMonitor) {
        return AjaxResult.success(this.sysMonitorService.update(sysMonitor));
    }

    @Log(title = "历史报警(", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysMonitor sysMonitor)
    {
        List<SysMonitor> list = sysMonitorService.queryByPage(sysMonitor);
        ExcelUtil<SysMonitor> util = new ExcelUtil<>(SysMonitor.class);
        util.exportExcel(response, list, "历史报警(");
    }

    /**
     * 确认报警
     *
     * @param sysMonitor 实体
     * @return 编辑结果
     */
    @PostMapping("/confirm")
    public AjaxResult confirm(@RequestBody SysMonitor sysMonitor) {
        sysMonitor.setMonitorConfirmState("已确认");
        sysMonitor.setMonitorConfirmTime(DateUtil.now());
        sysMonitor.setMonitorConfirmUser(SecurityUtils.getUsername());
        return AjaxResult.success(this.sysMonitorService.update(sysMonitor));
    }

    /**
     * 删除数据
     *
     * @param id 主键
     */
    @GetMapping("/delete")
    public AjaxResult deleteById(Integer id) {
        return AjaxResult.success(this.sysMonitorService.deleteById(id));
    }

    /**
     * @return
     */
    @GetMapping("/countByLevel")
    public AjaxResult countByLevel(String startTime, String endTime) {
        Map<String, Object> alertCount = sysMonitorService.getAlertCount(startTime, endTime);
        return AjaxResult.success(alertCount);
    }

    /*
    1.报警次数（饼图）：
        重大危险源
        关键部位
        可燃有毒气体
        环境监测
     */
    @GetMapping("/countByType")
    public AjaxResult countByType() {
        List<Map<String, Object>> list = sysMonitorService.countByType();
        return AjaxResult.success(list);
    }

    /*
        2.报警超时未处理列表：
        前10项
        报警点位
        报警参数
        报警时间
     */
    @GetMapping("/countByState")
    public AjaxResult countByState() {
        List<SysMonitor> list = sysMonitorService.countByState();
        return AjaxResult.success(list);
    }

    /*
        1.监测点位状态统计：
        监测点位数
        正常运行
        当前报警
     */
    @GetMapping("/countAll")
    public AjaxResult countAll() {
        Map<String, Object> map = sysMonitorService.countAll();
        return AjaxResult.success(map);
    }

    /*
        2.报警列表：
        报警设备
        报警参数
        报警时间
     */
    @GetMapping("/countAllAnomaly")
    public AjaxResult countAllAnomaly() {
        List<SysMonitor> list = sysMonitorService.countAllAnomaly();
        return AjaxResult.success(list);
    }

    /**
     * 历史报警统计
     * @return
     */
    @GetMapping("/countYearOrMonthOrDay")
    public AjaxResult countYearOrMonthOrDay(String startTime, String endTime) {
        Map<String, Object> alertCount = sysMonitorService.getCountYearOrMonthOrDay(startTime, endTime);
        return AjaxResult.success(alertCount);
    }

    /**
     * 顶部统计条排列：告警总数、告警处理超时数、重大危险源报警、可燃气体报警、生产数据报警、电子围栏告警、火灾报警、系统在线率 *
     * @return
     */
    @GetMapping("/countTop")
    public AjaxResult countTop() {
        // 告警总数总数
        int total = sysMonitorMapper.queryByMonitorType(null,null,null,null,null,null);
        // 重大危险源报警
        int danger = sysMonitorMapper.queryByMonitorType("重大危险源",null,null,null,null,null);
        // 可燃气体报警
        int gas = sysMonitorMapper.queryByMonitorType("可燃气体",null,null,null,null,null);
        // 生产数据报警
        int production = sysMonitorMapper.queryByMonitorType("生产数据",null,null,null,null,null);
        // 火灾报警
        int fire = sysMonitorMapper.queryByMonitorType("火灾报警",null,null,null,null,null);
        // 告警处理超时数
        SysMonitor sysMonitor = new SysMonitor();
        sysMonitor.setMonitorState("已超时");
        int timeouts = sysMonitorMapper.queryAllByLimit(sysMonitor).size();
        // 电子围栏告警
        Map<String,Integer> map = new HashedMap();
        map.put("告警总数",total);
        map.put("重大危险源报警",danger);
        map.put("可燃气体报警",gas);
        map.put("生产数据报警",production);
        map.put("火灾报警",fire);
        map.put("告警处理超时数",timeouts);
        return AjaxResult.success(map);
    }

    /**
     * 按系统（重大危险源、可燃气体、生产数据）以柱状图显示报警次数（柱状图按不同颜色标出HH、H、L、LL)，可以按时间段自定义查询*
     * @return
     */
    @GetMapping("/countByMonitorType")
    public AjaxResult countByType(String startTime,String endTime) {
        String[] arr = new String[]{"重大危险源","可燃气体","生产数据"};
        Map<String,Map<String,Integer>> map = new HashedMap();
        for (int i = 0; i < arr.length; i++) {
            // HH
            int hh = sysMonitorMapper.queryByMonitorType(arr[i],startTime,endTime,"高高报",null,null);
            // H
            int h = sysMonitorMapper.queryByMonitorType(arr[i],startTime,endTime,"高报",null,null);
            // L
            int l = sysMonitorMapper.queryByMonitorType(arr[i],startTime,endTime,"低报",null,null);
            // LL
            int ll = sysMonitorMapper.queryByMonitorType(arr[i],startTime,endTime,"低低报",null,null);
            Map<String,Integer> m = new HashedMap();
            m.put("ll",ll);
            m.put("l",l);
            m.put("h",h);
            m.put("hh",hh);
            map.put(arr[i],m);
        }
        return AjaxResult.success(map);
    }

    /**
     * 3、按月份以折线图方式展示报警变化（折线类型报包HH、H、L、LL,用不同颜色显示折线），可以按不同系统查询，不选则是默认所有系统一起查询*
     * @return
     */
    @GetMapping("/countByMonth")
    public AjaxResult countByMonth(String year,String monitorType) {
        String[] arr = new String[]{"01","02","03","04","05","06","07","08","09","10","11","12"};
        Map<String,Map<String,Integer>> map = new HashedMap();
        for (int i = 0; i < arr.length; i++) {
            // HH
            int hh = sysMonitorMapper.queryByMonitorType(monitorType,null,null,"高高报",year + "-" + arr[i],null);
            // H
            int h = sysMonitorMapper.queryByMonitorType(monitorType,null,null,"高报",year + "-" + arr[i],null);
            // L
            int l = sysMonitorMapper.queryByMonitorType(monitorType,null,null,"低报",year + "-" + arr[i],null);
            // LL
            int ll = sysMonitorMapper.queryByMonitorType(monitorType,null,null,"低低报",year + "-" + arr[i],null);
            Map<String,Integer> m = new HashedMap();
            m.put("ll",ll);
            m.put("l",l);
            m.put("h",h);
            m.put("hh",hh);
            map.put(arr[i],m);
        }
        return AjaxResult.success(map);
    }

    /**
     * 以环状或饼图方式展示报警中、报警超时、已恢复的报警数量及所占的百分比，可以按照系统，按时间段查询。*
     * @param startTime
     * @param endTime
     * @param monitorType
     * @return
     */
    @GetMapping("/countByStateAndMonitorType")
    public AjaxResult countByState(String startTime,String endTime,String monitorType) {
        // 未恢复
        int whf = sysMonitorMapper.queryByMonitorType(monitorType,startTime,endTime,null,null,"未恢复");
        // 已恢复
        int yhf = sysMonitorMapper.queryByMonitorType(monitorType,startTime,endTime,null,null,"已恢复");
        // 已超时
        int ycs = sysMonitorMapper.queryByMonitorType(monitorType,startTime,endTime,null,null,"已超时");
        Map<String,Integer> m = new HashedMap();
        m.put("未恢复",whf);
        m.put("已恢复",yhf);
        m.put("已超时",ycs);
        return AjaxResult.success(m);
    }
}